<?php
namespace app\web\model;

use cubox\Model;
use cubox\Cubox;

class Article extends Model{

    public function statusDefine(){
        return array(
           'new' => '新稿',
           'publish' => '已发布',
           'draft' => '草稿',
           'audit' => '审核中',
           'reject' => '审核拒绝',
           'recycle' => '回收站'
        );
    }

    public function formate($data){
        $data['create_datetime'] =  date('Y-m-d H:i:s',$data['create_time']);
        $data['update_datetime'] =  date('Y-m-d H:i:s',$data['update_time']);
        $statusArr = $this->statusDefine();
        $data['status_title'] = $statusArr[$data['status']]??'';
        return $data;
    }

    public function frontData($data,$partFrontData){

        try{
            $param = json_decode($data['param'],true);
        }catch(\Exception $e){
            $param = array();
        }

        $re = $param;
        $re['title'] =  $data['title'];
        $re['content']  = $data['content'];
        $re['title_style'] =  $data['title_style'];        
        $re['row'] = $data;
        $re['route'] = $this->getRoute($data,$partFrontData);
        
        return $re;
    }


    public function getRoute($data,$partFrontData){
        $route = $data['route'];
        if ($data['route_type']=='default'){
            if ($partFrontData['row']['route_type']=='default' || $partFrontData['row']['route_type']=='link'){
                $route = Cubox::url('web','article','read',['id'=>$data['article_id']]);
            }else{
                $route = $partFrontData['route'].'/'.$data['article_id'];
            }
        }

        if ($data['route_type']=='parent'){
            $route = $partFrontData['route'].'/'.$data['route'];
        }

        if ($data['route_type']=='root'){
            $route  = '/'.$data['route'];
        }

        return $route;
    }

}